package nxu.it.controller;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import nxu.it.Gender;
import nxu.it.Jiguan;
import nxu.it.Student;


import java.time.LocalDateTime;
import java.time.format.TextStyle;
import java.util.*;


@Path(value = "/", viewPath = "/")
public class MainController extends Controller {
    public void index() {
        renderHtml("<h2>It works!</h2>");
    }

    @ActionKey("/hello")
    public void hello() {

        String name = get("name");
        String age = get("age");
        if (name == null || name == "") {
            renderHtml("<h2>你好!无名氏</h2>");
        } else {
            renderHtml("<h2>你好!" + name + "</h2>");
        }
        Student[] students = {
                new Student("001","Hermione","Male",20),
                new Student("002","Lily","Male",20),
                new Student("003","Rose","Male",20),
                new Student("004","jack","Male",20)
        };
        set("students",students);
        set("age",age);
        renderFreeMarker("hello.ftl");
        // 二维码携带的数据
        String data = "<h2>你好!" + name + "</h2>";

        // 渲染二维码图片，长度与宽度为 200 像素
        renderQrCode(data, 200, 200);

    }

    @ActionKey("/bye")
    public void bye() {
        String name = get("name");
        if (name == null || name == "") {
            renderHtml("<h2>再见!无名氏</h2>");
        } else {
            renderHtml("<h2>再见!" + name + "</h2>");
        }
        // 二维码携带的数据
        String data = "<h2>再见!" + name + "</h2>";

        // 渲染二维码图片，长度与宽度为 200 像素
        renderQrCode(data, 200, 200);
    }

    public void today() {
        /*String jsonStr="{\"year\":year,\"month\":month,\"day\":day,\"hour\":hour,\"weekday\":weekday}";*/
        Map<String, Object> jsonData = new LinkedHashMap<>();
        LocalDateTime now = LocalDateTime.now();
        jsonData.put("year", now.getYear());
        jsonData.put("month", now.getMonthValue());
        jsonData.put("day", now.getDayOfMonth());
        jsonData.put("weekday", now.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.JAPAN));
        jsonData.put("hour", now.getHour());
        renderJson(jsonData);
    }

    public void register() {

        List<Jiguan> jiguans = List.of(
                new Jiguan("山西"),
                new Jiguan("宁夏"),
                new Jiguan("北京"),
                new Jiguan("上海"),
                new Jiguan("江苏"),
                new Jiguan("贵州"),
                new Jiguan("陕西")
        );
        set("jiguans", jiguans);
        set("habit1", "篮球");
        set("habit2", "足球");
        set("habit3", "羽毛球");
        set("habit4", "网球");
        set("habit5", "乒乓球");
        set("habit6", "排球");

        renderFreeMarker("register.ftl"); //注意 此处 字符串是要渲染的模板的路径 并非要输出的内容
        //renderTemplate("register.ftl"); //调用jfinal的模板输出  render("register.ftl");
        /*render("hello.html"); //和上面在*/
    }


    public void login() {
           renderCaptcha();
            renderFreeMarker("login.ftl");

    }

}
